package com.pda.common.pages.codereplace;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.pda.common.R;
import com.pda.common.basemvp.activity.BaseActivity;
import com.pda.common.beanin.CodeReplacrInBean;
import com.pda.common.broadcast.NetWorkChangReceiver;
import com.pda.common.scan.BroadcastConfig;
import com.pda.common.tools.SharedPreferencesManger;
import com.pda.common.tools.StringProcess;
import com.pda.common.tools.VoiceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodeReplaceActivity extends BaseActivity<CodeReplaceModel, CodeReplaceView, CodeReplacePresent> implements CodeReplaceView {
    private String TAG = "CodeResetActivity";
    @BindView(R.id.tvss)
    TextView tvss;
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvright)
    TextView tvright;
    @BindView(R.id.sws)
    Switch sws;
    @BindView(R.id.oet)
    EditText oet;
    @BindView(R.id.btquery)
    Button btquery;
    @BindView(R.id.tvq)
    TextView tvq;
    @BindView(R.id.net)
    EditText net;
    @BindView(R.id.btproduce)
    Button btproduce;
    @BindView(R.id.bt)
    Button bt;

    NetWorkChangReceiver netWorkChangReceiver;
    boolean flag = false;
    CodeReplacrInBean codeReplacrInBean = new CodeReplacrInBean();
    VoiceUtils voiceUtils;

    @Override
    protected void initdata() {
        tvTitle.setText("数码替换");
        sws.setChecked(false);
        sws.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    oet.setText("");
                    net.setText("");
                }
                flag = isChecked;
            }
        });
        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
        registerReceiver(mScanfBroadcastReceiver, intentFilter);

        netWorkChangReceiver = new NetWorkChangReceiver(new NetWorkChangReceiver.PriorityListener() {
            @Override
            public void setText(String string) {
                tvss.setText(string);
            }
        });
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netWorkChangReceiver, filter);
        codeReplacrInBean.setAction("ChangeProductionCode");
        codeReplacrInBean.setCid(SharedPreferencesManger.getCid());
        codeReplacrInBean.setUserID(SharedPreferencesManger.getUID());
        codeReplacrInBean.setStoreID(SharedPreferencesManger.getStoreID());
        voiceUtils = new VoiceUtils();
    }

    protected BroadcastReceiver mScanfBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastConfig.CUSTOM_NAME)) {
                String code = StringProcess.codeCL(intent.getStringExtra("scannerdata"));
                if (StringProcess.boxcodeJudge(code) || StringProcess.casecodeJudge(code)) {
                    if (flag) {
                        if (oet.getText().toString() == null || oet.getText().toString().length() == 0) {
                            showMsg("请输入旧数码");
                            return;
                        }
                        net.setText(code);
                        codeReplacrInBean.setOldCode(oet.getText().toString());
                        codeReplacrInBean.setNewCode(net.getText().toString());
                        presenter.codeReplace(codeReplacrInBean);
                    } else {
                        flag = true;
                        sws.setChecked(flag);
                        oet.setText(code);
                    }
                } else {
                    tvq.setText("条码长度不符");
                    voiceUtils.voiceWarn(mContext);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_replace);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(netWorkChangReceiver);
        unregisterReceiver(mScanfBroadcastReceiver);
    }

    @Override
    public CodeReplaceModel createModel() {
        return new CodeReplaceModelImpl(this);
    }

    @Override
    public CodeReplaceView createView() {
        return this;
    }

    @Override
    public CodeReplacePresent createPresenter() {
        return new CodeReplacePresent();
    }


    @Override
    public void tip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                flag = false;
                sws.setChecked(flag);
                oet.setText("");
                net.setText("");
                tvq.setText(str);
            }
        });
    }

    @OnClick({R.id.iv_title_left, R.id.btproduce, R.id.btquery, R.id.bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.bt:
                if (oet.getText().toString() == null || net.getText().toString().length() == 0) {
                    showMsg("请输入数码");
                    return;
                }
                codeReplacrInBean.setOldCode(oet.getText().toString());
                codeReplacrInBean.setNewCode(net.getText().toString());
                presenter.codeReplace(codeReplacrInBean);
                break;
        }
    }
}
